import config from './config.js';
import cache from './wcache.js';
import {
  get,
  post,
  put,
  del
} from './network';

class Api {
  constructor() {
    let self = this;
  }
  /* 获取缓存里面的session_id */
  static getSessionId() {
    //return wx.getStorageSync('session_id');
    return 'session_id';
  }

  // get
  static getPromise(url,params) {
    return new Promise((resolve, reject) => {
      get(url, params).then(data => {
        console.log("get-response-data:" + JSON.stringify(data));
        resolve(data);
      })
    });
  }

  // post
  static postPromise(url, params) {
    return new Promise((resolve, reject) => {
      post(url, params).then(data => {
        console.log("post-response-data:" + JSON.stringify(data));
        resolve(data);
      })
    });
  }

  // 会员资料get
  static MemberItem(params) {
    return new Promise((resolve, reject) => {
      get(config.MemberItem, params).then(data => {
        console.log("data:" + JSON.stringify(data));
        resolve(data);
      })
    });
  }


  /* 微信登录 */
  static wxlogin(code) {
    return new Promise((resolve, reject) => {
      resolve({})
    });
  }

  /* 保存用户信息 */
  static setUserInfo(userInfo) {
    return new Promise((resolve, reject) => {
      resolve({})
    });
  }

  /* 获取用户信息 */
  static getUserInfo() {
    return new Promise((resolve, reject) => {
      resolve({})
    });
  }

  /* 获取用户身份列表 */
  static getRoles(params) {
    return new Promise((resolve, reject) => {
      get(config.GetToken, params).then(data => {
        console.log("data:" + JSON.stringify(data));
        resolve(data);
      })
    });
  }

  static Authorization() {
    return new Promise((resolve, reject) => {
      var _authCode = cache.get("Authorization");
      if (_authCode) {
        cache.put("Authorization", _authCode, 3)
        resolve(cache.get("Authorization"))
      } else {
        get(config.GetToken, {
          ApiLoginName: config.ApiLoginName,
          ApiPwd: config.ApiLoginPwd
        }).then(data => {
          if (data.StatusCode != "n") {
            cache.put("Authorization", data.PayLoad, 3)
            resolve(data.PayLoad)
          } else {
            //获取AuthCode失败
            wx.showToast({
              title: "获取AuthCode失败"
            });
            reject(data)
          }
        })
      }
    });
  }

}

export default Api